<template>
  <st-table-panel>
    <div slot="search">
      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
        <el-form-item label="客户名称：" prop="customerName">
          <el-input
            v-model="queryParams.customerName"
            placeholder="请输入客户名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="客户性质：" prop="nature">
          <el-select
            v-model="queryParams.nature"
            placeholder="请输入客户性质"
            clearable
          >
            <el-option
              v-for="dict in dict.type.customer_nature"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="客户来源：" prop="source">
          <el-select
            v-model="queryParams.source"
            placeholder="请输入客户来源"
            clearable
          >
            <el-option
              v-for="dict in dict.type.customer_source"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div slot="btn">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            @click="handleAdd"
            v-hasPermi="['crm:customer:add']"
          >新增</el-button>
          <el-button
            type="success"
            plain
            icon="el-icon-edit"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['crm:customer:edit']"
          >修改</el-button>
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['crm:customer:remove']"
          >删除</el-button>
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            @click="handleExport"
            v-hasPermi="['crm:customer:export']"
          >导出</el-button>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </div>
    <div slot="content">
      <el-table v-loading="loading" border :height="tableHeight" :data="customerList" @selection-change="handleSelectionChange">
        <div slot="empty">
          <el-empty :image="noData">
          </el-empty>
        </div>
        <el-table-column type="selection" width="55" align="center" />
<!--        <el-table-column label="客户号" align="center" prop="customerId" />-->
        <el-table-column label="客户名称" align="center" prop="customerName" >
          <template slot-scope="scope">
            <el-button type="text"  @click="handleDetail(scope.row)">
              {{ scope.row.customerName }}
            </el-button>
          </template>
        </el-table-column>
        <el-table-column label="客户性质" align="center" prop="natureDisp" />
        <el-table-column label="客户来源" align="center" prop="sourceDisp" />
        <el-table-column label="客户行业" align="center" prop="industryDisp" />
        <el-table-column label="客户分级" align="center" prop="gradeDisp" />
        <el-table-column label="客户阶段" align="center" prop="stageDisp" />
        <el-table-column label="客户官网" align="center" prop="website" />
        <el-table-column label="客户类型" align="center" prop="typeDisp" />
        <el-table-column label="客户简介" align="center" prop="introduction" />
        <el-table-column label="重要程度" align="center" prop="importance" />
        <el-table-column label="销售负责人" align="center" prop="ownerUserId" />
        <el-table-column label="售后负责人" align="center" prop="saUserId" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['crm:customer:edit']"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['crm:customer:remove']"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 添加或修改客户对话框 -->
      <st-dialog :title="title"
                 v-if="open"
                 height="650px"
                 width="900px"
                 @close="cancel"
                 @save="submitForm"
                 append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="120px">
          <el-row>
            <el-col :span="11">
              <el-form-item label="客户名称：" prop="customerName">
                <el-input v-model="form.customerName" placeholder="请输入客户名称" />
              </el-form-item>
            </el-col>
            <el-col :span="11">
              <el-form-item label="客户性质：" prop="nature">
                <el-select v-model="form.nature" placeholder="请输入客户性质">
                  <el-option
                    v-for="dict in dict.type.customer_nature"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="11">
              <el-form-item label="客户来源：" prop="source">
                <el-select v-model="form.source" placeholder="请输入客户来源">
                  <el-option
                    v-for="dict in dict.type.customer_source"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="11">
              <el-form-item label="客户行业：" prop="industry">
                <el-select v-model="form.industry" placeholder="请输入客户行业">
                  <el-option
                    v-for="dict in dict.type.customer_industry"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="11">
              <el-form-item label="客户分级：" prop="grade">
                <el-select v-model="form.grade" placeholder="请输入客户分级">
                  <el-option
                    v-for="dict in dict.type.customer_grade"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="11">
              <el-form-item label="客户阶段：" prop="stage">
                <el-select v-model="form.stage" placeholder="请输入客户阶段">
                  <el-option
                    v-for="dict in dict.type.customer_stage"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="11">
              <el-form-item label="客户类型：" prop="importance">
                <el-select v-model="form.type" placeholder="请输入客户类型">
                  <el-option
                    v-for="dict in dict.type.customer_type"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="11">
              <el-form-item label="客户官网：" prop="website">
                <el-input v-model="form.website" placeholder="请输入客户官网" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="11">
              <el-form-item label="重要程度：" prop="importance">
                <el-rate
                  style="text-align: left;margin-top:5px;"
                  v-model="form.importance">
                </el-rate>
              </el-form-item>
            </el-col>
            <el-col :span="11">
              <el-form-item label="销售负责人：" prop="ownerUserId">
                <el-input v-model="form.ownerUserId" placeholder="请输入销售负责人" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="11">
              <el-form-item label="售后负责人：" prop="saUserId">
                <el-input v-model="form.saUserId" placeholder="请输入售后负责人" />
              </el-form-item>
            </el-col>
            <el-col :span="11">
              <el-form-item label="下次联系时间：" prop="saUserId">
                <el-date-picker
                  v-model="form.nextContactTime"
                  type="date"
                  placeholder="请输入下次联系时间">
                </el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="15">
              <el-form-item label="所属区域：">
                <st-distpicker
                  :province="areaData[0]"
                  :city="areaData[1]"
                  :area="areaData[2]"
                  @province="selectProvince"
                  @city="selectCity"
                  @area="selectArea"/>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="22">
              <el-form-item label="详细地址：" prop="address">
                <el-input v-model="form.address" type="textarea" placeholder="请输入内容" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="22">
              <el-form-item label="客户简介：" prop="introduction">
                <el-input v-model="form.introduction" type="textarea" placeholder="请输入内容" />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="22">
              <el-form-item label="备注：" prop="remark">
                <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </st-dialog>
      <detail v-if="showDetail" ref="detailRef" :params="detailParams" @close="showDetail=false"></detail>
    </div>
    <div slot="footer">
      <pagination
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>

  </st-table-panel>
</template>
<script>
import { listCustomer, getCustomer, delCustomer, addCustomer, updateCustomer } from "@/api/crm/customer";
import noData from '@/assets/img/no_data.png'
import detail from './detail/index'
import StDistpicker from '@/components/StDistpicker'
export default {
  name: "Customer",
  components:{
    StDistpicker,
    detail
  },
  dicts: ['customer_nature',
    'customer_source',
    'customer_industry',
    'customer_grade',
    'customer_stage',
    'customer_type'
  ],
  data() {
    return {
      showDetail:false,
      noData:noData,
      areaData:[],
      activeName: 'first',
      tableHeight:undefined,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客户表格数据
      customerList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        customerName: null,
        nature: null,
        source: null,
        industry: null,
        grade: null,
        stage: null,
        website: null,
        type: null,
        introduction: null,
        importance: null,
        lockStatus: null,
        address: null,
        provinceId: null,
        cityId: null,
        areaId: null,
        ownerUserId: null,
        saUserId: null,
        deltag: null,
        createdBy: null,
        createdTime: null,
        updatedBy: null,
        updatedTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      detailParams:{}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleClick(tab, event) {
      console.log(tab, event);
    },
    handleDetail(row){
      this.showDetail=true;
      this.detailParams=Object.assign('',row);
      /*this.$nextTick(()=>{
        this.$refs.detailRef.show(row);
      })*/
    },
    selectProvince(value) {
      this.$set(this.form,'provinceId',value.code)
    },
    selectCity(value) {
      this.$set(this.form,'cityId',value.code)
    },
    selectArea(value) {
      this.$set(this.form,'areaId',value.code)
    },
      /** 查询客户列表 */
    getList() {
      this.loading = true;
      listCustomer(this.queryParams).then(response => {
        this.customerList = response.rows;
        this.total = response.total;
        this.loading = false;
        this.$nextTick(() => {
          this.tableHeight = this.getTableHeight();
        })
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.areaData.length=0;
      this.form = {
        customerId: null,
        customerName: null,
        nature: null,
        source: null,
        industry: null,
        grade: null,
        stage: null,
        website: null,
        type: null,
        introduction: null,
        importance: null,
        lockStatus: null,
        address: null,
        provinceId: null,
        cityId: null,
        areaId: null,
        ownerUserId: null,
        saUserId: null,
        deltag: null,
        remark: null,
        createdBy: null,
        createdTime: null,
        updatedBy: null,
        updatedTime: null

      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.customerId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加客户";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const customerId = row.customerId || this.ids
      getCustomer(customerId).then(response => {
        this.form = response.data;
        if(typeof this.form.provinceId != "undefined" && this.form.provinceId != null) {
          this.$set(this.areaData,0,+this.form.provinceId)
        }
        if(typeof this.form.cityId != "undefined" && this.form.cityId != null) {
          this.$set(this.areaData,1,+this.form.cityId)
        }
        if(typeof this.form.areaId != "undefined" && this.form.areaId != null) {
          this.$set(this.areaData,2,+this.form.areaId)
        }
        this.open = true;
        this.title = "修改客户";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.customerId != null) {
            updateCustomer(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCustomer(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const customerIds = row.customerId || this.ids;
      this.$modal.confirm('是否确认删除客户编号为"' + customerIds + '"的数据项？').then(function() {
        return delCustomer(customerIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('crm/customer/export', {
        ...this.queryParams
      }, `customer_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style scoped lang="scss">
.el-form{
  text-align: center;
}
.el-select {
  width : 100%;
}
.el-cascader{
  width: 100%;
}
.el-date-editor{
  width: 100%;
}
::v-deep .el-tabs--border-card > .el-tabs__content{
  padding: 0px !important;
}
</style>
